package StreamDemo01;





import java.util.*;
import java.util.function.DoubleToIntFunction;
import java.util.stream.Collectors;

public class Test {
    public static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

        public static void main(String[] args) {
            List<Person> people1 = Arrays.asList(
                    new Person("Alice", 30),
                    new Person("Bob", 20),
                    new Person("Charlie", 25),
                    new Person("David", 30),
                    new Person("Eve", 20)

            );
            // 1. 过滤对象
            List<Person> list = people1.stream().filter(abc->abc.getAge()>19).collect(Collectors.toList());
            System.out.println("list:"+list);

            // 2. 映射对象属性
            List<String> list1 = people1.stream().map(Person::getName).collect(Collectors.toList());
            System.out.println("list1" + list1);

            // 3. 排序对象
            List<Person> list2 = people1.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
            System.out.println("list2"+list2);

            // 4. 去重对象
            List<Person> list3 = people1.stream().distinct().collect(Collectors.toList());
            System.out.println("list3"+list3);

            // 5. 分组对象
            Map<Integer ,List<Person>> listMap = people1.stream().collect(Collectors.groupingBy(Person::getAge));
            System.out.println("listMap"+listMap);

            // 6. 统计对象数量
            long count = people1.stream().count();
            System.out.println("count"+ count);

            // 7. 计算最大/最小值
            Optional<Person> list4 = people1.stream().max(Comparator.comparing(Person::getAge));
            System.out.println("list4"+list4);

            // 8. 连接属性
            String name = people1.stream().map(Person::getName).collect(Collectors.joining(","));
            System.out.println("name"+name);

            // 9. 合并对象属性
            int allAge = people1.stream().map(Person::getAge).reduce(0,Integer::sum);
            System.out.println("allAge"+allAge);

            // 10. 计算平均值
            double averAge = people1.stream().mapToInt(Person::getAge).average().orElse(0);
            System.out.println("averAge"+ averAge);

            // 11. 获取流的第一个元素
            Optional<Person> list5 = people1.stream().findFirst();
            System.out.println("list5"+list5);

            // 12. 获取流的任意一个元素（并行流时有用）
            Optional<Person> list6 = people1.stream().findAny();
            System.out.println("list6"+list6);

            // 13. 检查是否所有元素匹配条件
            boolean allAdults = people1.stream().allMatch(Person->Person.getAge()>18);
            System.out.println("allAdults"+allAdults);

            // 14. 检查是否有任何元素匹配条件
            boolean anyAdults = people1.stream().anyMatch(Person->Person.getAge()<18);
            System.out.println("anyAdults"+anyAdults);

            // 15. 检查是否没有任何元素匹配条件
            boolean noAdults = people1.stream().noneMatch(Person->Person.getAge()<0);
            System.out.println("noAdults"+noAdults);

            // 16. 映射到一个新的流，并进行收集（例如: 对象到列表的转换）
            List<String> list7 = people1.stream().map(Person->Person.getName().toUpperCase()).collect(Collectors.toList());
            System.out.println("list7"+list7);

            // 17. 将流中的元素聚合为一个 Map
            Map<String,Integer> map = people1.stream().collect(Collectors.toMap(Person::getName,Person::getAge));
            System.out.println("map"+map);

            // 18. 将流中的元素转换为一个 Set
            Set<String> set = people1.stream().map(Person::getName).collect(Collectors.toSet());
            System.out.println("set"+set);

            // 19. 获取流中最大值和最小值（示例：获取最高和最低年龄）
            Optional<Person> max = people1.stream().max(Comparator.comparing(Person::getAge));
            System.out.println("max"+max);

            Optional<Person> min = people1.stream().min(Comparator.comparing(Person::getAge));
            System.out.println("min"+min);
        }
    }

}
